package com.example.threaddemo;


import java.util.concurrent.*;

public class MyThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(2,
                                                        5,
                                                        1L,
                                                        TimeUnit.SECONDS,
                                                        new LinkedBlockingQueue<>(3),
                                                        Executors.defaultThreadFactory(),
//                                                        new ThreadPoolExecutor.AbortPolicy());
//                                                        new ThreadPoolExecutor.CallerRunsPolicy());
                                                        new ThreadPoolExecutor.DiscardOldestPolicy());
//                                                        new ThreadPoolExecutor.DiscardPolicy());
        int count = 0;

        try {
            for (int i = 0; i < 20; i++) {
                final int tempCount = count++;
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "    正在处理任务" + ";  执行任务的数量是：" + tempCount);
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
